#!/bin/bash

# usage: bash compare_two_lineups.sh schedule_file_name new_lineup_file_name old_lineup_file_name
# Examples:
# bash compare_two_lineups.sh /metadata/Redbee/be/20130903190005samsung.xml BE_Telenet_TVA_Lineups_20130530_v8.xml BE_Telenet_TVA_Lineups_20130530v2.xml
# bash compare_two_lineups.sh /metadata/Redbee/nl/20130903190306samsung_.xml NL_Ziggo_TVA_Lineups_20130411v15.xml NL_Ziggo_TVA_Lineups_20130411v5.xml

source func_module.eclass

function get_normalized_line {
  local LINE=$1
  local TAG=$2
  local HEADEND_ID=""
  local DEV_CODE=""
  local LCN=""
  local ONID=""
  local TSID=""
  local SID=""
  local FREQ=""
  local CHANNEL_ID=""
  local CHANEL_NAME=""

  HEADEND_ID=`echo $LINE | awk -F "|" '{print $1}' | perl -pe 's/ //g'`
  DEV_CODE=`echo $LINE | awk -F "|" '{print $2}' | perl -pe 's/ //g' | tr '[:lower:]' '[:upper:]'`
  LCN=`echo $LINE | awk -F "|" '{print $3}' | perl -pe 's/ //g'`
  ONID=`echo $LINE | awk -F "|" '{print $4}' | perl -pe 's/ //g' | tr '[:upper:]' '[:lower:]' | perl -pe 's/0x//g'`
  TSID=`echo $LINE | awk -F "|" '{print $5}' | perl -pe 's/ //g' | tr '[:upper:]' '[:lower:]' | perl -pe 's/0x//g'`
  SID=`echo $LINE | awk -F "|" '{print $6}' | perl -pe 's/ //g' | tr '[:upper:]' '[:lower:]' | perl -pe 's/0x//g'`
  FREQ=`echo $LINE | awk -F "|" '{print $7}' | perl -pe 's/ //g'`
  CHANNEL_ID=`echo $LINE | awk -F "|" '{print $8}' | perl -pe 's/ //g'`  # Currently this info will not be used.
  CHANNEL_NAME=`echo $LINE | awk -F "|" '{print $9}' | perl -pe 's/^\s* //g' | perl -pe 's/\s*\n/\n/g'`
  echo $HEADEND_ID"|"$DEV_CODE"|"$LCN"|"$ONID"|"$TSID"|"$SID"| "$FREQ"|"$CHANNEL_ID"|"$CHANNEL_NAME"|"$TAG
}

# This function generates the field lineup files according to the headend id and device type.
# Remove the white space in the LCN, DVB-Triplet and Frequency.  
# Change the capital letter to lower case in DVB-Tripet data.
function get_normalized_field_data {
  local FIELD_DATA_FILE_TXT=$1
  local OUTPUTDIR=$2
  local HEADEND_ID=""
  local DEV_CODE=""
  local IS_NOT_DATA=""
  local NORM_FILES=""
  local LINE=""

  NORM_FILES=(${OUTPUTDIR}/*-field.txt)

  if [ -e ${NORM_FILES[0]} ]; then
    rm ${OUTPUTDIR}/*-field.txt
  fi

  while read LINE
  do
    # IS_NOT_DATA=`echo $LINE | grep "^Headend" |awk '{print $1}'`
    # $10: Exception field: If this field has any data, we do not add this line for checking.
    IS_NOT_DATA=`echo $LINE | awk -F "|" '{print $10}'`

    if [ -z $IS_NOT_DATA ]; then
      HEADEND_ID=`echo $LINE | awk -F "|" '{print $1}' | perl -pe 's/ //g'`
      DEV_CODE=`echo $LINE | awk -F "|" '{print $2}' | perl -pe 's/ //g' | tr '[:lower:]' '[:upper:]'`

      get_normalized_line "$LINE" "&" >> $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-tmp-field.txt

      echo $HEADEND_ID"|"$DEV_CODE >> $OUTPUTDIR/tmp_headendlist.txt
    fi
  done < $FIELD_DATA_FILE_TXT

  cat ${OUTPUTDIR}/tmp_headendlist.txt | sort -u > $OUTPUTDIR/target_headend_list.txt
  rm $OUTPUTDIR/tmp_headendlist.txt

  while read LINE
  do
    HEADEND_ID=`echo $LINE | awk -F "|" '{print $1}'`
    DEV_CODE=`echo $LINE | awk -F "|" '{print $2}'`
    # add line without duplication
    cat $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-tmp-field.txt | sort -k1,1 > $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-tmp-field-t1.txt
    cat $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-tmp-field-t1.txt | uniq -u > $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-tmp-field.txt
    cat $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-tmp-field-t1.txt | uniq -d >> $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-tmp-field.txt
    cat $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-tmp-field.txt | sort -k1,1 > $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-field.txt
    rm $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-tmp-field.txt
    rm $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-tmp-field-t1.txt
  done < $OUTPUTDIR/target_headend_list.txt

}

function check_headend_in_lineup {
  local TARGET_HEADEND_LIST=$1
  local LINEUP_FILE_XML=$2
  local OUTPUTDIR=$3
  local FIELD_DATA_FILE_ONLY=`basename $FIELD_DATA_FILE_TXT`
  local FIELD_DATA_FILE="${FIELD_DATA_FILE_ONLY%.*}"
  local IS_EXIST=""

  get_headend_info $LINEUP_FILE_XML > ${OUTPUTDIR}/${LINEUP_FILE}_Lineups.txt

  if [ -e $OUTPUTDIR/target_headend_list_common.txt ]; then
    rm $OUTPUTDIR/target_headend_list_common.txt
  fi

  while read line
  do
    HEADEND_ID=`echo $line | awk -F "|" '{print $1}'`  
    DEV_CODE=`echo $line | awk -F "|" '{print $2}'`  
    IS_EXIST=`cat ${OUTPUTDIR}/${LINEUP_FILE}_Lineups.txt |grep "$HEADEND_ID|$DEV_CODE" |awk '{print $1}'`
    if [ -z $IS_EXIST ]; then
      echo "Headend: "$HEADEND_ID"("$DEV_CODE") does not exsit in the lineup!" 
    else
      echo $HEADEND_ID"|"$DEV_CODE >> $OUTPUTDIR/target_headend_list_common.txt
    fi
  done < $TARGET_HEADEND_LIST
}

function get_normalized_lineup_in_lineupfile {
  local TARGET_HEADEND_COMMON=$1 
  local LINEUP_FILE_XML=$2
  local LINEUP_FILE_ONLY=`basename $LINEUP_FILE_XML`
  local LINEUP_FILE="${LINEUP_FILE_ONLY%.*}"
  local OUTPUTDIR=$3
  local HEADEND_ID=""
  local DEVICE_TYPE_CODE=""
  local LINE_A=""
  local LINE_B=""
  local NORM_FILES=""
  
  NORM_FILES=(${OUTPUTDIR}/*-metadata.txt)

  if [ -e ${NORM_FILES[0]} ]; then
    rm ${OUTPUTDIR}/*-metadata.txt
  fi

  while read LINE_A
  do
    HEADEND_ID=`echo $LINE_A | awk -F "|" '{print $1}'`
    DEVICE_TYPE_CODE=`echo $LINE_A | awk -F "|" '{print $2}'`
    check_each_lineup $LINEUP_FILE_XML $HEADEND_ID $DEVICE_TYPE_CODE > ${OUTPUTDIR}/lineup_${LINEUP_FILE}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ALL.txt

    while read LINE_B
    do
      get_normalized_line "$LINE_B" "*" >> $OUTPUTDIR/${HEADEND_ID}-${DEVICE_TYPE_CODE}-tmp-metadata.txt 
    done < ${OUTPUTDIR}/lineup_${LINEUP_FILE}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ALL.txt

    cat $OUTPUTDIR/${HEADEND_ID}-${DEVICE_TYPE_CODE}-tmp-metadata.txt | sort -k1,1 > $OUTPUTDIR/${HEADEND_ID}-${DEVICE_TYPE_CODE}-metadata.txt
    rm $OUTPUTDIR/${HEADEND_ID}-${DEVICE_TYPE_CODE}-tmp-metadata.txt
  done < $TARGET_HEADEND_COMMON
}

function main {
  local FIELD_DATA_FILE_TXT=$1
  local FIELD_DATA_FILE_ONLY=`basename $FIELD_DATA_FILE_TXT`
  local FIELD_DATA_FILE="${FIELD_DATA_FILE_ONLY%.*}"

  local LINEUP_FILE_XML=$2
  #LINEUP_FILE_NEW_XML="BE_Telenet_TVA_Lineups_20130530_v8.xml"
  local LINEUP_FILE_ONLY=`basename $LINEUP_FILE_XML`
  local LINEUP_FILE="${LINEUP_FILE_ONLY%.*}"
  local LINE=""

  local OUTPUTDIR="field-"$FIELD_DATA_FILE"-"${LINEUP_FILE}
  if [ ! -d "$OUTPUTDIR" ]; then
    mkdir $OUTPUTDIR 
  fi

  get_normalized_field_data $FIELD_DATA_FILE_TXT $OUTPUTDIR

  check_headend_in_lineup $OUTPUTDIR/target_headend_list.txt $LINEUP_FILE_XML $OUTPUTDIR

  get_normalized_lineup_in_lineupfile $OUTPUTDIR/target_headend_list_common.txt $LINEUP_FILE_XML $OUTPUTDIR

  if [ -e ${OUTPUTDIR}/report-matched.txt ]; then
    rm ${OUTPUTDIR}/report*.txt 
  fi

  while read LINE
  do
    HEADEND_ID=`echo $LINE | awk -F "|" '{print $1}'`
    DEV_CODE=`echo $LINE | awk -F "|" '{print $2}'`

    #join $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-field.txt $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-metadata.txt >> $OUTPUTDIR/report-matched.txt
    #join $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-field.txt $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-metadata.txt -a 1 | grep -v "|\*"  >> $OUTPUTDIR/report-field_only.txt
    #join $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-field.txt $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-metadata.txt -a 2 | grep -v "|&" >> $OUTPUTDIR/report-metadata_only.txt
    python2 comp_meta_field.py -m $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-metadata.txt -f $OUTPUTDIR/${HEADEND_ID}-${DEV_CODE}-field.txt -e equ_channels.csv -o $OUTPUTDIR

  done < $OUTPUTDIR/target_headend_list_common.txt

  echo "---- Field only ----" > $OUTPUTDIR/report-summary.txt
  cat $OUTPUTDIR/report-field_only.txt >> $OUTPUTDIR/report-summary.txt
  echo "---- metadata only (for tuner case we can ignore this) ----" >> $OUTPUTDIR/report-summary.txt
  cat $OUTPUTDIR/report-metadata_only.txt >> $OUTPUTDIR/report-summary.txt
  echo "---- matched Channels ----" >> $OUTPUTDIR/report-summary.txt
  cat $OUTPUTDIR/report-matched.txt >> $OUTPUTDIR/report-summary.txt
  echo "---- mismatched only Channel name ----" >> $OUTPUTDIR/report-summary.txt
  cat $OUTPUTDIR/report-only_ch_name_mismatched.txt >> $OUTPUTDIR/report-summary.txt

}

# $1: SCHEDULE_FILE_XML $2: $LINEUP_FILE_NEW_XML $3: $LINEUP_FILE_OLD_XML
main $1 $2

